package cn.iocoder.yudao.module.dine.product.service.admin;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.service.CrudService;
import cn.iocoder.yudao.framework.business.basic.service.impl.BaseService;
import cn.iocoder.yudao.module.dine.product.convert.DineProductConvert;
import cn.iocoder.yudao.module.dine.product.dao.DineProductMapper;
import cn.iocoder.yudao.module.dine.product.pojo.dto.DineProductQueryDTO;
import cn.iocoder.yudao.module.dine.product.pojo.dto.DineProductSaveDTO;
import cn.iocoder.yudao.module.dine.product.pojo.po.DineProduct;
import cn.iocoder.yudao.module.dine.product.pojo.vo.DineProductVO;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserLastShopId;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 服务
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/07/18
 */
@Service
public class DineProductService extends BaseService<DineProduct, DineProductVO, DineProductQueryDTO> implements CrudService<DineProduct, DineProductVO, DineProductSaveDTO, DineProductQueryDTO> {
    @Resource
    private DineProductMapper dineProductMapper;

    @Override
    public DineProductMapper getInjectedMapper() {
        return dineProductMapper;
    }

    @Nonnull
    @Override
    protected CoreQueryWrapper<DineProduct> buildQueryWrapper(DineProductQueryDTO param) {
        CoreQueryWrapper<DineProduct> wrapper = super.buildQueryWrapper(param);
        if (StrUtil.isNotBlank(param.getKeyword())) {
            wrapper.and(it -> {
                it.like("title", param.getKeyword());
                it.or();
                it.like("labels", param.getKeyword());
            });
        }
        wrapper.eqIfPresent("category_id", param.getCategoryId());
        wrapper.eqIfPresent("attribute", param.getAttribute());
        wrapper.eqIfPresent("status", param.getStatus());
        wrapper.eqIfPresent("required", param.getRequired());
        wrapper.eqIfPresent("upscale", param.getUpscale());
        wrapper.eqIfPresent("reservable", param.getReservable());
        if (param.getFree() != null) {
            if (param.getFree()) {
                wrapper.gt("free_quantity", 0);
            } else {
                wrapper.le("free_quantity", 0);
            }
        }
        return wrapper;
    }

    @Override
    public DineProductVO convertToViewObject(DineProduct entity) {
        return DineProductConvert.INSTANCE.toViewObject(entity);
    }

    @Override
    public DineProduct convertToEntity(DineProductSaveDTO param) {
        return DineProductConvert.INSTANCE.toEntity(param);
    }

    @Override
    public void beforeCreate(DineProduct entity, @Nullable Long userId) {
        entity.setShopId(getLoginUserLastShopId());
    }

}
